﻿#include "XpBpHelper.h"

bool XpBpHelper::g_loadXpBp = false;
DYNLIB_HANDLE XpBpHelper::g_hXpmdl = NULL;
DYNLIB_HANDLE XpBpHelper::g_hBpmdl = NULL;

KCXP_Disconn_proc	XpBpHelper::KCXP_Disconn = NULL;
KCXP_Close_proc	XpBpHelper::KCXP_Close = NULL;
KCXP_Conn_proc XpBpHelper::KCXP_Conn = NULL;
KCXP_Connx_proc XpBpHelper::KCXP_Connx = NULL;
KCXP_Inq_proc XpBpHelper::KCXP_Inq = NULL;
KCXP_Open_proc XpBpHelper::KCXP_Open = NULL;
KCXP_ErrInfo_proc XpBpHelper::KCXP_ErrInfo = NULL;

KCXP_Put_proc XpBpHelper::KCXP_Put = NULL;
KCXP_Get_proc XpBpHelper::KCXP_Get = NULL;

KCBPOP_Init_proc XpBpHelper::KCBPOP_Init = NULL;
KCBPOP_Exit_proc XpBpHelper::KCBPOP_Exit = NULL;

KCBPOP_BeginWrite_proc XpBpHelper::KCBPOP_BeginWrite = NULL;
KCBPOP_EndWrite_proc XpBpHelper::KCBPOP_EndWrite = NULL;

KCBPOP_PutKCBPPacket_proc XpBpHelper::KCBPOP_PutKCBPPacket = NULL;
KCBPOP_GetKCBPPacket_proc XpBpHelper::KCBPOP_GetKCBPPacket = NULL;
//KCBPOP_FetchKCBPPacket_proc XpBpHelper::KCBPOP_FetchKCBPPacket = NULL;
KCBPOP_GetKCBPPacketLen_proc XpBpHelper::KCBPOP_GetKCBPPacketLen = NULL;

KCBPOP_GetValue_proc XpBpHelper::KCBPOP_GetValue = NULL;
KCBPOP_SetValueStr_proc XpBpHelper::KCBPOP_SetValueStr = NULL;

KCBPOP_GetErr_proc XpBpHelper::KCBPOP_GetErr = NULL;

KCBPOP_GetValueNum_proc XpBpHelper::KCBPOP_GetValueNum = NULL;
KCBPOP_GetValueNameByIndex_proc XpBpHelper::KCBPOP_GetValueNameByIndex = NULL;
KCBPOP_GetValueAndNameByIndex_proc XpBpHelper::KCBPOP_GetValueAndNameByIndex = NULL;

KCBPOP_GetVal_proc	 XpBpHelper::KCBPOP_GetVal = NULL;

KCBPOP_SetSystemParam_proc XpBpHelper::KCBPOP_SetSystemParam = NULL;
KCBPOP_GetSystemParam_proc XpBpHelper::KCBPOP_GetSystemParam = NULL;

XpBpHelper::XpBpHelper()
{
}

XpBpHelper::~XpBpHelper()
{
}

bool XpBpHelper::InitalizeXpBp()
{
	if (g_loadXpBp) return true;

	if (g_hXpmdl) DYNLIB_UNLOAD(g_hXpmdl);
	if (!(g_hXpmdl = DYNLIB_LOADEX(TEXT("libkcxpmq.so"))))
	{
#ifdef ARK_PLATFORM_WIN
		char buffer[2048];
		DWORD error = GetLastError();
		FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, 2048, NULL);
		std::cerr << buffer << std::endl;
#else
		fprintf(stderr, "dlopen get error: %s\n", dlerror());
#endif
		return false;
	}

	if (g_hBpmdl) DYNLIB_UNLOAD(g_hBpmdl);
	if (!(g_hBpmdl = DYNLIB_LOADEX(TEXT("libkcbppacketopapi.so"))))
	{
		fprintf(stderr, "dlopen get error: %s\n", dlerror());
		return false;
	}

	// 加载KCXP接口函数
	KCXP_Disconn = (KCXP_Disconn_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Disconn");
	KCXP_Close = (KCXP_Close_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Close");
	KCXP_Conn = (KCXP_Conn_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Conn");
	KCXP_Connx = (KCXP_Connx_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Connx");
	KCXP_Inq = (KCXP_Inq_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Inq");
	KCXP_Open = (KCXP_Open_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Open");
	KCXP_ErrInfo = (KCXP_ErrInfo_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_ErrInfo");

	KCXP_Put = (KCXP_Put_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Put");
	KCXP_Get = (KCXP_Get_proc)DYNLIB_GETSYM(g_hXpmdl, "KCXP_Get");

	if (KCXP_Connx && KCXP_Open &&
		KCXP_ErrInfo && KCXP_Put &&
		KCXP_Get && KCXP_Close &&
		KCXP_Disconn && KCXP_Conn &&
		KCXP_Inq
		)
	{
		// 加载KCXP函数
		g_loadXpBp = true;
	}
	else
	{
		g_loadXpBp = false;
		return false;
	}

	// 加载KCBP接口函数
	KCBPOP_Init = (KCBPOP_Init_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_Init");
	KCBPOP_Exit = (KCBPOP_Exit_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_Exit");

	KCBPOP_BeginWrite = (KCBPOP_BeginWrite_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_BeginWrite");
	KCBPOP_EndWrite = (KCBPOP_EndWrite_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_EndWrite");

	KCBPOP_PutKCBPPacket = (KCBPOP_PutKCBPPacket_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_PutKCBPPacket");
	KCBPOP_GetKCBPPacket = (KCBPOP_GetKCBPPacket_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetKCBPPacket");
	//KCBPOP_FetchKCBPPacket = (KCBPOP_FetchKCBPPacket_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_FetchKCBPPacket");
	KCBPOP_GetKCBPPacketLen = (KCBPOP_GetKCBPPacketLen_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetKCBPPacketLen");

	KCBPOP_GetValue = (KCBPOP_GetValue_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetValue");
	KCBPOP_SetValueStr = (KCBPOP_SetValueStr_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_SetValueStr");

	KCBPOP_GetErr = (KCBPOP_GetErr_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetErr");

	KCBPOP_GetValueNum = (KCBPOP_GetValueNum_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetValueNum");
	KCBPOP_GetValueNameByIndex = (KCBPOP_GetValueNameByIndex_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetValueNameByIndex");
	KCBPOP_GetValueAndNameByIndex = (KCBPOP_GetValueAndNameByIndex_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetValueAndNameByIndex");

	KCBPOP_GetVal = (KCBPOP_GetVal_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetVal");

	KCBPOP_SetSystemParam = (KCBPOP_SetSystemParam_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_SetSystemParam");
	KCBPOP_GetSystemParam = (KCBPOP_GetSystemParam_proc)DYNLIB_GETSYM(g_hBpmdl, "KCBPOP_GetSystemParam");

	if (KCBPOP_Init && KCBPOP_Exit && KCBPOP_BeginWrite && KCBPOP_EndWrite &&
		KCBPOP_PutKCBPPacket && KCBPOP_GetKCBPPacket && KCBPOP_GetKCBPPacketLen &&
		KCBPOP_GetValue && KCBPOP_SetValueStr && KCBPOP_GetErr && KCBPOP_GetValueNum &&
		KCBPOP_GetValueNameByIndex && KCBPOP_GetValueAndNameByIndex && KCBPOP_GetVal &&
		KCBPOP_SetSystemParam && KCBPOP_GetSystemParam
		)
	{
		// 加载KCXP函数
		g_loadXpBp = true;
	}
	else
	{
		g_loadXpBp = false;
	}

	return g_loadXpBp;
}

bool XpBpHelper::UnInitalizeXpBp()
{
	if (g_hXpmdl) DYNLIB_UNLOAD(g_hXpmdl);
	if (g_hBpmdl) DYNLIB_UNLOAD(g_hBpmdl);
	g_hXpmdl = nullptr;
	g_hBpmdl = nullptr;

	return true;
}